class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> tem = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        backTracking(result, tem, nums, set);
        return result;
    }

    public void backTracking(List<List<Integer>> result, List<Integer> tem, int[] nums, Set<Integer> set) {
        if (tem.size() == nums.length) {
            result.add(new ArrayList<>(tem));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            tem.add(nums[i]);
            backTracking(result, tem, nums, set);
            tem.remove(tem.size() - 1);
            set.remove(nums[i]);

        }
    }
}
